# coding:utf8
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

from collections import defaultdict


class _Components(object):
    components = {
        "components": {
            "schemas": {

            },
            "parameters": {

            },
            "securitySchemes": {

            },
            "responses": {

            }
        }
    }

    default_schemas = {
        "SuccessReturn": {
            "type": "object",
            "properties": {
                "code": {
                    "type": "integer",
                    "example": 1
                },
                "msg": {
                    "type": "string",
                    "example": "ok"
                }
            }
        },
        "TokenError": {
            "type": "object",
            "properties": {
                "code": {
                    "type": "integer",
                    "example": 2
                },
                "msg": {
                    "type": "string",
                    "example": "Access token required"
                }
            }
        },
        "SystemVersion": {
            "type": "object",
            "properties": {
                "version": {
                    "type": "string",
                    "example": "2.0.8.2"
                }
            }
        },
        "License": {
            "type": "object",
            "properties": {
                "maxHosts": {
                    "type": "integer",
                    "example": 50
                },
                "maxConnections": {
                    "type": "integer",
                    "example": 50
                },
                "usedHosts": {
                    "type": "integer",
                    "example": 13
                },
                "usedConnections": {
                    "type": "integer",
                    "example": 8
                }
            }
        },
        "SyslogConfig": {
            "type": "object",
            "properties": {
                "tag": {
                    "type": "string",
                    "example": "usm"
                },
                "server": {
                    "type": "string",
                    "example": "1.1.1.1"
                },
                "port": {
                    "type": "integer",
                    "example": 514
                }
            }
        },
        "OmConfig": {
            "type": "object",
            "properties": {
                "file": {
                    "type": "object",
                    "properties": {
                        "enableBuildSha1": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableSave": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableSaveCompress": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableSaveDownload": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableSaveMaximumLimit": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableSaveUpload": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableSingleSaveMaximumLimit": {
                            "type": "boolean",
                            "example": True
                        },
                        "saveMaximum": {
                            "type": "integer",
                            "example": 30
                        },
                        "singleSaveMaximumLimit": {
                            "type": "integer",
                            "example": 100
                        }
                    }
                },
                "rdp": {
                    "type": "object",
                    "properties": {
                        "enableClipboardRedirection": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableDiskRedirection": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableKeyboardRecord": {
                            "type": "boolean",
                            "example": True
                        }
                    }
                },
                "session": {
                    "type": "object",
                    "properties": {
                        "enableHistoryRecord": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableLiveRecord": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableNotes": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableSecondaryApprove": {
                            "type": "boolean",
                            "example": True
                        }
                    }
                },
                "ssh": {
                    "type": "object",
                    "properties": {
                        "enableExecCommand": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableSftpTunnel": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableTcpForwarding": {
                            "type": "boolean",
                            "example": True
                        },
                        "enableX11Forwarding": {
                            "type": "boolean",
                            "example": True
                        }
                    }
                }
            }
        },
        "Protocol": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "enum": [
                        "SSH",
                        "TELNET",
                        "FTP",
                        "SFTP",
                        "VNC",
                        "RDP",
                        "SQLSERVER",
                        "MYSQL",
                        "ORACLE",
                        "DB2",
                        "RLOGIN"
                    ]
                },
                "port": {
                    "type": "integer",
                    "example": 22
                }
            }
        },
        "ParamsHost": {
            "type": "object",
            "properties": {
                "hostIp": {
                    "type": "string",
                    "example": "1.1.1.1"
                },
                "hostName": {
                    "type": "string",
                    "example": "主机名称"
                },
                "departmentId": {
                    "type": "integer",
                    "example": 3
                },
                "comment": {
                    "type": "string",
                    "example": "主机备注"
                },
                "enable": {
                    "type": "boolean",
                    "example": True
                },
                "configs": {
                    "type": "object",
                    "allOf": [
                        {
                            "$ref": "#/components/schemas/OmConfig"
                        }
                    ]
                },
                "protocols": {
                    "type": "array",
                    "items": {
                        "$ref": "#/components/schemas/Protocol"
                    }
                }
            }
        },
        "Host": {
            "type": "object",
            "allOf": [
                {
                    "$ref": "#/components/schemas/ParamsHost"
                },
                {
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "integer",
                            "example": 2
                        }
                    }
                }
            ]
        },
        "Hosts": {
            "type": "array",
            "items": {
                "$ref": "#/components/schemas/Host"
            }
        },
        "ParamsAccount": {
            "type": "object",
            "properties": {
                "accountName": {
                    "type": "string",
                    "example": "root"
                },
                "authMode": {
                    "type": "string",
                    "enum": [
                        "autoLogin",
                        "secondLogin",
                        "manualLogin"
                    ]
                },
                "password": {
                    "type": "string",
                    "example": "123456"
                },
                "privateKey": {
                    "type": "string",
                    "example": ""
                },
                "privateKeyPassphrase": {
                    "type": "string",
                    "example": "123456"
                },
                "privilegeAccount": {
                    "type": "boolean",
                    "example": True
                },
                "protocol": {
                    "type": "string",
                    "enum": [
                        "SSH",
                        "TELNET",
                        "FTP",
                        "SFTP",
                        "VNC",
                        "RDP",
                        "SQLSERVER",
                        "MYSQL",
                        "ORACLE",
                        "DB2",
                        "RLOGIN"
                    ]
                },
                "switchCommand": {
                    "type": "string",
                    "example": "su"
                },
                "switchFromAccountId": {
                    "type": "integer",
                    "example": 4
                },
                "usingPrivilegeAccountChangePassword": {
                    "type": "boolean",
                    "example": True
                },
                "connectionRole": {
                    "type": "string",
                    "enum": [
                        "NORMAL",
                        "SYSASM",
                        "SYSDBA",
                        "SYSOPER"
                    ]
                },
                "serviceName": {
                    "type": "string",
                    "example": "db1"
                },
                "connectionAttribute": {
                    "type": "string",
                    "enum": [
                        "SID",
                        "SERVICENAME"
                    ]
                }
            }
        },
        "Account": {
            "type": "object",
            "allOf": [
                {
                    "$ref": "#/components/schemas/ParamsAccount"
                },
                {
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "integer",
                            "example": 2
                        }
                    }
                }
            ]
        },
        "Accounts": {
            "type": "array",
            "items": {
                "$ref": "#/components/schemas/Account"
            }
        },
        "OmReport": {
            "type": "object",
            "properties": {
                "omCount": {
                    "type": "integer",
                    "example": 20
                },
                "omAppCount": {
                    "type": "integer",
                    "example": 20
                },
                "omSSHCount": {
                    "type": "integer",
                    "example": 20
                },
                "omTELNETCount": {
                    "type": "integer",
                    "example": 20
                },
                "omRDPCount": {
                    "type": "integer",
                    "example": 20
                },
                "omVNCCount": {
                    "type": "integer",
                    "example": 20
                },
                "omFTPCount": {
                    "type": "integer",
                    "example": 20
                },
                "omSFTPCount": {
                    "type": "integer",
                    "example": 20
                },
                "omSQLServerCount": {
                    "type": "integer",
                    "example": 20
                },
                "omMySQLCount": {
                    "type": "integer",
                    "example": 20
                },
                "omOracleCount": {
                    "type": "integer",
                    "example": 20
                },
                "omDB2Count": {
                    "type": "integer",
                    "example": 20
                },
                "omRloginCount": {
                    "type": "integer",
                    "example": 20
                },
                "omDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omAppDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omSSHDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omTELNETDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omRDPDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omVNCDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omFTPDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omSFTPDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omSQLServerDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omMySQLDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omOracleDuration": {
                    "type": "integer",
                    "example": 20
                },
                "omDB2Duration": {
                    "type": "integer",
                    "example": 20
                },
                "omRloginDuration": {
                    "type": "integer",
                    "example": 20
                }
            }
        }
    }

    default_parameters = {
        "PageLimit": {
            "name": "limit",
            "in": "query",
            "description": "Limits the number of items on a page",
            "schema": {
                "type": "integer"
            }
        },
        "PageOffset": {
            "name": "offset",
            "in": "query",
            "description": "Specifies the page number of the artists to be displayed",
            "schema": {
                "type": "integer"
            }
        }
    }

    default_security = {
        "AccessToken": {
            "type": "apiKey",
            "name": "AccessToken",
            "in": "header"
        }
    }

    default_responses = {
        "400Error": {
            "description": "Invalid request",
            "content": {
                "application/json": {
                    "schema": {
                        "type": "object",
                        "properties": {
                            "message": {"type": "string"}
                        }
                    }
                }
            }
        }
    }

    refs = defaultdict(str)

    @classmethod
    def push_schemas(cls, key, data):
        if isinstance(data, dict):
            key_data = "#/components/schemas/{}".format(key)
            cls.refs[key] = key_data
            cls.components['components']['schemas'][key] = data

    @classmethod
    def push_parameters(cls, key, data):
        # print key,data
        if isinstance(data, dict):
            key_data = "#/components/parameters/{}".format(key)
            cls.refs[key] = key_data
            cls.components['components']['parameters'][key] = data

    @classmethod
    def push_responses(cls, key, data):
        if isinstance(data, dict):
            key_data = "#/components/responses/{}".format(key)
            cls.refs[key] = key_data
            cls.components['components']['responses'][key] = data

    @classmethod
    def push_securitySchemes(cls, key, data):
        if isinstance(data, dict):
            key_data = "#/components/securitySchemes/{}".format(key)
            cls.refs[key] = key_data
            cls.components['components']['securitySchemes'][key] = data

    _is_init = False

    @classmethod
    def init_components(cls):
        if not cls._is_init:
            for k, v in cls.default_parameters.items():
                _Components.push_parameters(k, v)

            for k, v in cls.default_security.items():
                _Components.push_securitySchemes(k, v)

            for k, v in cls.default_schemas.items():
                _Components.push_schemas(k, v)

            for k, v in cls.default_responses.items():
                _Components.push_responses(k, v)
            cls._is_init = True


